package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.interceptor.LoginInterceptor;
import com.atguigu.gmall.cart.pojo.Cart;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.bean.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping
    public String addCart(Cart cart){
        if (cart == null || cart.getSkuId() == null){
            throw new RuntimeException("没有选择添加到购物车的商品信息！");
        }
        this.cartService.addCart(cart);
        return "redirect:http://cart.gmall.com/addCart.html?skuId=" + cart.getSkuId() + "&count=" + cart.getCount();
    }

    @GetMapping("addCart.html")
    public String queryCartBySkuId(Cart cart, Model model){
        BigDecimal count = cart.getCount();
        cart = this.cartService.queryCartBySkuId(cart);
        cart.setCount(count);
        model.addAttribute("cart",cart);
        return "addCart";

    }

    @PostMapping("updateNum")
    @ResponseBody
    public ResponseVo<Object> updateNum(@RequestBody Cart cart){

        this.cartService.updateNum(cart);
        return ResponseVo.ok();
    }

    @PostMapping("deleteCart")
    @ResponseBody
    public ResponseVo<Object> deleteCart(@RequestParam("skuId")Long skuId){

        this.cartService.deleteCart(skuId);
        return ResponseVo.ok();
    }

//    @ResponseBody // 先响应json测试通过后，再加入页面联调
@GetMapping("cart.html")
public String queryCarts(Model model) {

    List<Cart> carts = this.cartService.queryCarts();
    model.addAttribute("carts", carts);
    return "cart";
}

    @GetMapping("test1")
    @ResponseBody
    public String test(HttpServletRequest request){
        System.out.println(LoginInterceptor.getUserInfo());
        return "hello cart!";
    }

    @GetMapping("test")
    @ResponseBody
    public String test() throws ExecutionException, InterruptedException {
        long now = System.currentTimeMillis();
        System.out.println("controller.test方法开始执行！");
        this.cartService.executor2();
        System.out.println("controller.test方法结束执行！！！" + (System.currentTimeMillis() - now));

        return "hello cart!";
    }

//    @GetMapping("test")
//    @ResponseBody
//    public String test() throws ExecutionException, InterruptedException {
//        long now = System.currentTimeMillis();
//        System.out.println("controller.test方法开始执行！");
//        this.cartService.executor1().addCallback(new SuccessCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                System.out.println("future1的正常执行结果：" + result);
//            }
//        }, new FailureCallback() {
//            @Override
//            public void onFailure(Throwable ex) {
//                System.out.println("future1执行出错：" + ex.getMessage());
//            }
//        });
//        this.cartService.executor2().addCallback(new SuccessCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                System.out.println("future2的正常执行结果：" + result);
//            }
//        }, new FailureCallback() {
//            @Override
//            public void onFailure(Throwable ex) {
//                System.out.println("future2执行出错：" + ex.getMessage());
//            }
//        });
//        System.out.println("controller.test方法结束执行！！！" + (System.currentTimeMillis() - now));
//
//        return "hello cart!";
//    }


    @GetMapping("check/{userId}")
    @ResponseBody
    public ResponseVo<List<Cart>> queryCheckedCarts(@PathVariable("userId") Long userId) {
        List<Cart> carts = this.cartService.queryCheckedCarts(userId);
        return ResponseVo.ok(carts);
    }

}
